package com.csc.movie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csc.movie.mapper.MovieMapper;
import com.csc.movie.model.entity.Movie;
import com.csc.movie.model.vo.MovieVO;
import com.csc.movie.service.MovieService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MovieServiceImpl extends ServiceImpl<MovieMapper, Movie> implements MovieService {
    @Autowired
    private MovieMapper movieMapper;

    @Override
    public Page<MovieVO> getMovicesByCondition(MovieVO movieVO, Integer pageNum, Integer pageSize) {
        Page<MovieVO> page = new Page<>(pageNum,pageSize);
        //1、条件分页查询借款列表
        QueryWrapper<Movie> wrapper = new QueryWrapper<>();


        String name = movieVO.getName();
        String type = movieVO.getType();
        log.error(name);
        if(StringUtils.isNotEmpty(name)){
//            wrapper.and((t->t.like("m1.name",name)));
//            wrapper.like("m1.name",name);
//            wrapper.and(t-> t.like("name", name));
            wrapper.like("name",name);
        }

        if(StringUtils.isNotEmpty(type)){
            wrapper.eq("type",type);
        }

        wrapper.orderByAsc("id");

        List<MovieVO> movieVOS = baseMapper.selectMovieVOs(page, wrapper);
        page.setRecords(movieVOS);
        return page;
    }

    @Override
    public void saveMovie(MovieVO movieVO) {
        Movie movie=new Movie();
        BeanUtils.copyProperties(movieVO,movie);
        this.save(movie);
    }
}
